/*******************************************************************************
 * Copyright (c) 2013 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package com.niannian.util;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import android.database.Cursor;

public class Cursor2VO {
	private static Map<Class, Class> basicMap = new HashMap<Class, Class>();
	static {
		basicMap.put(int.class, Integer.class);
		basicMap.put(long.class, Long.class);
		basicMap.put(float.class, Float.class);
		basicMap.put(double.class, Double.class);
		basicMap.put(boolean.class, Boolean.class);
		basicMap.put(byte.class, Byte.class);
		basicMap.put(short.class, Short.class);
	}

	public static List cursor2VOList(Cursor c, Class clazz) {
		if (c == null) {
			return null;
		}
		List list = new LinkedList();
		Object obj;
		try {
			while (c.moveToNext()) {
				obj = setValues2Fields(c, clazz);
				list.add(obj);
			}
			return list;
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("ERROR @：cursor2VOList");
			return null;
		} finally {
			c.close();
		}
	}

	private static Object setValues2Fields(Cursor c, Class clazz)
			throws Exception {
		String[] columnNames = c.getColumnNames();// 字段数组
		Object obj = clazz.newInstance();
		Field[] fields = clazz.getDeclaredFields();
		for (Field _field : fields) {
			if (Modifier.isFinal(_field.getModifiers()))
				continue;
			Class<? extends Object> typeClass = _field.getType();// 属性类型
			for (int j = 0; j < columnNames.length; j++) {
				String columnName = columnNames[j];
				typeClass = getBasicClass(typeClass);
				boolean isBasicType = isBasicType(typeClass);
				if (isBasicType) {
					if (columnName.equalsIgnoreCase(_field.getName())) {// 是基本类型
						String _str = c.getString(c.getColumnIndex(columnName));
						if (_str == null) {
							break;
						}
						_str = _str == null ? "" : _str;
						FileUtils.setValueToFiled(_field, obj, _str);
						break;
					}
				} /*
				 * else { Object obj2 = setValues2Fields(c, typeClass);// 递归
				 * _field.set(obj, obj2); break; }
				 */

			}
		}
		return obj;
	}

	private static boolean isBasicType(Class typeClass) {
		if (typeClass.equals(Integer.class) || typeClass.equals(Long.class)
				|| typeClass.equals(Float.class)
				|| typeClass.equals(Double.class)
				|| typeClass.equals(Boolean.class)
				|| typeClass.equals(Byte.class)
				|| typeClass.equals(Short.class)
				|| typeClass.equals(String.class)) {

			return true;

		} else {
			return false;
		}
	}

	/**
	 * 获得包装类
	 * 
	 * @param typeClass
	 * @return
	 */
	@SuppressWarnings("all")
	private static Class<? extends Object> getBasicClass(Class typeClass) {
		Class _class = basicMap.get(typeClass);
		if (_class == null)
			_class = typeClass;
		return _class;
	}

}
